import Vue from 'vue';
import {
  Message
} from 'element-ui';
import {
  api_GET_Dict,
  api_GET_District
} from '@/extend/api/swaggerApi/common';
import {
  api_GET_Dict as api_GET_Dict_Game
} from '@/extend/api/gameswaggerApi/common';
import {
  api_GET_Dict as api_GET_Dict_Train
} from '@/extend/api/trainswaggerApi/common';
import {
  api_GET_Dict as api_GET_Dict_Shoppingmall
} from '@/extend/api/shoppingmallswaggerApi/common';
import {
  api_GET_Dict as api_GET_Dict_Hall
} from '@/extend/api/hallswaggerApi/common';
import {
  api_GET_Editions
} from '@/extend/api/swaggerApi/saas';
import {
  api_GET_EventsManagementCompetitionLevel,
  api_GET_EventsManagementCompetitionCertificationLevel
} from '@/extend/api/gameswaggerApi/app';
const state = {
  // 存放根据类型获取的数据
  cache: {},
  cacheAll: {},
  // 存放基础字典
  getDictCMD: false,
  dict: {},
  dictAll: {},
  dictGender: [{
    code: 'M',
    value: '男'
  },
  {
    code: 'W',
    value: '女'
  }
  ],
  dictRelationship: [{
    code: '父子/父女',
    value: '父子/父女'
  },
  {
    code: '母子/母女',
    value: '母子/母女'
  },
  {
    code: '兄弟/兄妹',
    value: '兄弟/兄妹'
  },
  {
    code: '姐弟/姐妹',
    value: '姐弟/姐妹'
  },
  {
    code: '其他',
    value: '其他'
  }],
  dictCtaBeltLevel: [ // 中跆协级位
    {
      code: 0,
      value: '无'
    },
    {
      code: 1,
      value: '1级'
    },
    {
      code: 2,
      value: '2级'
    },
    {
      code: 3,
      value: '3级'
    },
    {
      code: 4,
      value: '4级'
    },
    {
      code: 5,
      value: '5级'
    },
    {
      code: 6,
      value: '6级'
    },
    {
      code: 7,
      value: '7级'
    },
    {
      code: 8,
      value: '8级'
    },
    {
      code: 9,
      value: '9级'
    },
    {
      code: 10,
      value: '10级'
    }
  ],
  dictCtaBeltDuan: [ // 中跆协段位
    {
      code: 0,
      value: '无'
    },
    {
      code: 1,
      value: '1段'
    },
    {
      code: 2,
      value: '2段'
    },
    {
      code: 3,
      value: '3段'
    },
    {
      code: 4,
      value: '4段'
    },
    {
      code: 5,
      value: '5段'
    },
    {
      code: 6,
      value: '6段'
    },
    {
      code: 7,
      value: '7段'
    },
    {
      code: 8,
      value: '8段'
    },
    {
      code: 9,
      value: '9段'
    }
  ],
  dictWtaBeltLevel: [ // 国际台协段位
    {
      code: 0,
      value: '无'
    },
    {
      code: 1,
      value: '1段'
    },
    {
      code: 2,
      value: '2段'
    },
    {
      code: 3,
      value: '3段'
    },
    {
      code: 4,
      value: '4段'
    },
    {
      code: 5,
      value: '5段'
    },
    {
      code: 6,
      value: '6段'
    },
    {
      code: 7,
      value: '7段'
    },
    {
      code: 8,
      value: '8段'
    },
    {
      code: 9,
      value: '9段'
    }
  ]
};

const mutations = {
  // 设置字典
  SET_DICT: (state, data) => {
    if (!state.dict) {
      state.dict = data;
    }
    for (const i in data) {
      Vue.set(state.dict, i, data[i]);
      const _data = JSON.parse(JSON.stringify(data[i]));
      const _paramsAll = {};
      if (_data[0]) {
        for (const j in _data[0]) {
          if (/[\u4e00-\u9fa5]/.test(_data[0][j]) || j === 'name') {
            _paramsAll[j] = '全部';
          } else {
            _paramsAll[j] = '';
          }
        }
      }
      _data.unshift(_paramsAll);
      Vue.set(state.dictAll, i, _data);
    }
  },
  // 根据类型设置字典
  SET_DICT_BY_TYPE: (state, data) => {
    Vue.set(state.cache, data.type, data.data);
    const _data = JSON.parse(JSON.stringify(data.data));
    const _paramsAll = {};
    if (_data[0]) {
      for (const j in _data[0]) {
        if (/[\u4e00-\u9fa5]/.test(_data[0][j]) || j === 'name') {
          _paramsAll[j] = '全部';
        } else {
          _paramsAll[j] = '';
        }
      }
    }
    _data.unshift(_paramsAll);
    Vue.set(state.cacheAll, data.type, _data);
  },
  SET_GET_DICT_CMD: (state, data) => {
    state.getDictCMD = data;
  }
};
const actions = {
  /**
   * 获取基础字典
   * @param { string } code (code === 'reset')是否重新获取
   * @returns { object } 获取到的字典
   **/
  getDict: ({
    commit,
    state
  }, code) => {
    return new Promise((resolve, reject) => {
      // 重新获取基础字典
      if (code === 'reset' || JSON.stringify(state.dict) === '{}') {
        if (!state.getDictCMD) {
          commit('SET_GET_DICT_CMD', true);
          Promise.all([
            actions.api_GET_Dict(),
            actions.api_GET_Dict_Game(),
            actions.api_GET_Dict_Train(),
            actions.api_GET_Dict_Shoppingmall(),
            actions.api_GET_Dict_Hall()
          ]).then(res => {
            const _dict = {};
            const dictMap = {
              dictGender: [{
                code: 'M',
                value: '男'
              },
              {
                code: 'W',
                value: '女'
              }
              ],
              dictRelationship: [{
                code: '父子/父女',
                value: '父子/父女'
              },
              {
                code: '母子/母女',
                value: '母子/母女'
              },
              {
                code: '兄弟/兄妹',
                value: '兄弟/兄妹'
              },
              {
                code: '姐弟/姐妹',
                value: '姐弟/姐妹'
              },
              {
                code: '其他',
                value: '其他'
              }
              ],
              dictRounds: [{
                code: '1',
                value: '决赛'
              },
              {
                code: '2',
                value: '第二轮'
              },
              {
                code: '3',
                value: '第三轮'
              },
              {
                code: '4',
                value: '第四轮'
              }
              ]
            };
            // Object.assign(_dict, res[0], res[1], res[2], res[3], dictMap);
            Object.assign(_dict, ...res, dictMap);
            commit('SET_DICT', _dict);
            resolve(_dict);
          });
        } else {
          resolve(state.dict);
        }
      } else {
        resolve(state.dict);
      }
    });
  },
  // 获取字典 https://tfs.ydfuture.com:44307
  api_GET_Dict() {
    return new Promise((resolve, reject) => {
      if (api_GET_Dict) {
        api_GET_Dict().then(res => {
          resolve(res);
        }, error => {
          reject(error);
        });
      } else {
        resolve([]);
      }
    });
  },
  // 获取字典 https://tfs.ydfuture.com:44308
  api_GET_Dict_Game() {
    return new Promise((resolve, reject) => {
      if (api_GET_Dict_Game) {
        api_GET_Dict_Game().then(res => {
          resolve(res);
        }, error => {
          reject(error);
        });
      } else {
        resolve([]);
      }
    });
  },
  // 获取字典 https://tfs.ydfuture.com:44309
  api_GET_Dict_Train() {
    return new Promise((resolve, reject) => {
      if (api_GET_Dict_Train) {
        api_GET_Dict_Train().then(res => {
          resolve(res);
        }, error => {
          reject(error);
        });
      } else {
        resolve([]);
      }
    });
  },
  // 获取字典 https://tfs.ydfuture.com:44310
  api_GET_Dict_Shoppingmall() {
    return new Promise((resolve, reject) => {
      if (api_GET_Dict_Shoppingmall) {
        api_GET_Dict_Shoppingmall().then(res => {
          resolve(res);
        }, error => {
          reject(error);
        });
      } else {
        resolve([]);
      }
    });
  },
  // 获取字典 https://tfs.ydfuture.com:44312
  api_GET_Dict_Hall() {
    return new Promise((resolve, reject) => {
      if (api_GET_Dict_Hall) {
        api_GET_Dict_Hall().then(res => {
          resolve(res);
        }, error => {
          reject(error);
        });
      } else {
        resolve([]);
      }
    });
  },
  // 字典设置
  getDictByType: ({
    dispatch,
    state
  }, dictTypeList) => {
    dictTypeList.forEach(item => {
      switch (item) {
        case 'district':
          if (!(state.cache && state.cache[item])) {
            dispatch('getDistrict');
          }
          break;
        case 'editions':
          if (!(state.cache && state.cache[item])) {
            dispatch('getEditions');
          }
          break;
        case 'competitionLevel':
          if (!(state.cache && state.cache[item])) {
            dispatch('getCompetitionLevel');
          }
          break;
        case 'competitionCertificationLevel':
          if (!(state.cache && state.cache[item])) {
            dispatch('getCompetitionCertificationLevel');
          }
          break;
        default:
          break;
      }
    });
  },
  // 获取所有的行政区树
  getDistrict: ({
    commit
  }) => {
    return new Promise((resolve, reject) => {
      api_GET_District().then(res => {
        commit('SET_DICT_BY_TYPE', {
          type: 'district',
          data: res
        });
        resolve(res);
      }, error => {
        Message({
          type: 'error',
          message: `获取所有的行政区树失败！${error.error.message}`
        });
      });
    });
  },
  // 获取所有的版本
  getEditions: ({
    commit
  }) => {
    return new Promise((resolve, reject) => {
      api_GET_Editions().then(res => {
        commit('SET_DICT_BY_TYPE', {
          type: 'editions',
          data: res
        });
        resolve(res);
      }, error => {
        Message({
          type: 'error',
          message: `获取所有的版本失败！${error.error.message}`
        });
      });
    });
  },
  // 获取赛事级别列表
  getCompetitionLevel: ({
    commit
  }) => {
    return new Promise((resolve, reject) => {
      api_GET_EventsManagementCompetitionLevel().then(res => {
        commit('SET_DICT_BY_TYPE', {
          type: 'competitionLevel',
          data: res.items
        });
        resolve(res.items);
      }, error => {
        Message({
          type: 'error',
          message: `获取赛事级别列表失败！${error.error.message}`
        });
      });
    });
  },
  // 获取认证等级列表
  getCompetitionCertificationLevel: ({
    commit
  }) => {
    return new Promise((resolve, reject) => {
      api_GET_EventsManagementCompetitionCertificationLevel({ IsLocked: false }).then(res => {
        commit('SET_DICT_BY_TYPE', {
          type: 'competitionCertificationLevel',
          data: res.items
        });
        resolve(res.items);
      }, error => {
        Message({
          type: 'error',
          message: `获取认证等级列表失败！${error.error.message}`
        });
      });
    });
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
